package com.adlotto2.lcdns.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.adlotto2.lcdns.R;

public class BKLocationManager implements LocationListener {
		
	public static final int REQUEST_ADDRESS     = 0;
	public static final int REQUEST_COORDINATES = 1;
	
	public interface LocationStateListener {
		public abstract void onLocationGettingtFinished(Location location, BKAddress address);
		public abstract void onLocationGettingError(String reason);
	}
	
	private Context mContext;
	
	private BKAddress mAddress;
	
	private LocationManager mLocationManager;
	
	private LocationStateListener mLocationListener;
	
	private Location mLocation;
	
	private boolean mIsContinue; // GPS가 꺼진상태에서도 사용자가 진행할지 여부를 판단하는 체크플래그.
	
	public BKLocationManager(Context con) {
		mContext = con;
		mIsContinue = false;
		mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		mLocationListener = new LocationStateListener() {

			@Override
			public void onLocationGettingtFinished(Location location,
					BKAddress address) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLocationGettingError(String reason) {
				// TODO Auto-generated method stub
				
			}

		};
	}
	
	public void requestCurrentLocations() {
		
		if(!isGpsEnabled() && !mIsContinue) {
			showGpsAlert();
			Log.i("BenTag", "GPS Off, not Continue");
			return;
		}
		
		mIsContinue = false;
		Criteria criteria = new Criteria();
		
		String provider = mLocationManager.getBestProvider(criteria, true);
		if(provider == null || !isGpsEnabled()){ // get address via network when GPS module is off.
			Toast.makeText(mContext, "no GPS Provider", Toast.LENGTH_SHORT).show();
			provider = LocationManager.NETWORK_PROVIDER;
			mLocation = mLocationManager.getLastKnownLocation(provider);
			mAddress = new BKAddress(mLocation, mContext);
			Log.d("BenTag", "1");
			mLocationListener.onLocationGettingtFinished(mLocation, mAddress);
			showAddressLog();
		} else {
			/*mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, BKLocationManager.this, null);*/
//			mLocationManager.requestLocationUpdates(provider, 0, 0, this);
//			mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
		}
		
	}
	
	
	public void setOnLocationStateListener(LocationStateListener listener) {
		mLocationListener = listener;
	}
	
	
	
	public boolean isGpsEnabled() {
		if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			return true;
		} else {
			return false;
		}
	}
	
	private void showGpsAlert() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(mContext.getString(R.string.popup_alert)).setMessage(mContext.getString(R.string.gps_disabled_warning)).
		setPositiveButton(R.string.btn_title_yes, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				showGpsOptions();
			}
		}).
		setNegativeButton(R.string.btn_title_no, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				mIsContinue = true;
				requestCurrentLocations();
			}
		});
		
		dialog.show();
	}
	
	private void showGpsOptions() {
		Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		mContext.startActivity(intent);
	}
	

	@Override
	public void onLocationChanged(Location location) {
		Log.d("BenTag", "onLocationChanged");
		mLocation = location;
		mAddress = new BKAddress(mLocation, mContext);
		Log.d("BenTag", "2");
		mLocationListener.onLocationGettingtFinished(mLocation, mAddress);
		showAddressLog();
	}
	
	
	
	// 테스트용.. 실제 코드에서는 삭제
	public void showAddressLog() {
		Log.i("BenTag", "This Phone is be near here : " + mAddress.getFullAddress());
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		Log.d("BenTag", "onProviderDisabled : " + provider);
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		Log.d("BenTag", "onProviderEnabled + " + provider);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		Log.d("BenTag", "onStatusChanged : " + provider + ", status : " + status + ", extras : " + extras.toString());
	}
	
	
}
